﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows.Forms;
using OpenWings.Entities;
using OpenWings.UI.Controls;
using OpenWings.Engine;

namespace OpenWings.UI
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        Config _CurrentConfig = Config.Current;

        private void MainForm_Load(object sender, EventArgs e)
        {
            _CurrentConfig.Load("Servers.xml", "Clients.xml");
            _CurrentConfig.DirtyChanged += new EventHandler<DirtyChangedEventArgs>(_CurrentConfig_DirtyChanged);
            _CurrentConfig.DirtyTracking = true;

            ServersView.GroupNames = Enum.GetNames(typeof(ServerType));
            GroupsView.Groups = _CurrentConfig.ServerConfig.Groups;
            ClientsView.Clients = _CurrentConfig.ClientConfig.Clients;

            ListViewModeButtonLogic();
        }

        void _CurrentConfig_DirtyChanged(object sender, DirtyChangedEventArgs e)
        {
            SaveMenuItem.Enabled = e.Dirty;
            SaveButton.Enabled = e.Dirty;                        
        }

        void ListViewModeButtonLogic()
        {
            DetailsDisplayMenuItem.Checked = ServersView.View == View.Details;
            ListDisplayMenuItem.Checked = ServersView.View == View.List;
            IconsDisplayMenuItem.Checked = ServersView.View == View.LargeIcon;
        }

        private void DetailsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ServersView.View = View.Details;
            ListViewModeButtonLogic();
        }

        private void ListToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ServersView.View = View.List;
            ListViewModeButtonLogic();
        }

        private void IconsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ServersView.View = View.LargeIcon;
            ListViewModeButtonLogic();
        }

        private void GroupContextMenu_Opening(object sender, CancelEventArgs e)
        {
            if (GroupsView.SelectedGroup == null)
                e.Cancel = true;
        }

        private void ExitMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void SaveMenuItem_Click(object sender, EventArgs e)
        {
            SaveConfig();
        }

        private void SaveButton_Click(object sender, EventArgs e)
        {
            SaveConfig();
        }

        void SaveConfig()
        {
            _CurrentConfig.Save();
        }

        private void AboutMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void GroupsView_SelectGroup(object sender, GroupEventArgs e)
        {
            if (e.Group == null)
                ServersView.Servers = null;
            else
                ServersView.Servers = e.Group.Servers;

            PropertiesView.Entity = e.Group;
        }

        private void GroupsView_EditGroup(object sender, GroupEventArgs e)
        {
            MainSplitContainer.Panel2Collapsed = false;
            PropertiesView.Entity = e.Group;
        }

        private void GroupsView_AddGroup(object sender, GroupEventArgs e)
        {
            Group SelectedGroup = GroupsView.SelectedGroup;

            if (SelectedGroup == null)
                _CurrentConfig.ServerConfig.Groups.Add(e.Group);
            else
                SelectedGroup.Groups.Add(e.Group);
        }

        private void GroupsView_DeleteGroup(object sender, GroupEventArgs e)
        {
            Group SelectedGroup = GroupsView.SelectedGroup;

            if (SelectedGroup != null)
                if (e.Group == null)
                    _CurrentConfig.ServerConfig.Groups.Remove(SelectedGroup);
                else
                    e.Group.Groups.Remove(SelectedGroup);
        }

        private void ServersView_SelectServer(object sender, ServerEventArgs e)
        {
            ClientsView.LaunchWithServer(e.Server);
            PropertiesView.Entity = e.Server;
        }

        private void ServersView_EditServer(object sender, ServerEventArgs e)
        {
            MainSplitContainer.Panel2Collapsed = false;
            PropertiesView.Entity = e.Server;
        }

        private void ServersView_AddServer(object sender, ServerEventArgs e)
        {
            Group SelectedGroup = GroupsView.SelectedGroup;

            if (SelectedGroup != null)
                SelectedGroup.Servers.Add(e.Server);

            MainSplitContainer.Panel2Collapsed = false;
            PropertiesView.Entity = e.Server;
        }

        private void ServersView_DeleteServer(object sender, ServerEventArgs e)
        {
            Group SelectedGroup = GroupsView.SelectedGroup;

            if (SelectedGroup != null)
                SelectedGroup.Servers.Remove(e.Server);
        }

        private void ClientsView_LaunchClient(object sender, ClientEventArgs e)
        {
            if (ServersView.SelectedServer != null)
                Launcher.LaunchClient(ServersView.SelectedServer, e.Client);
        }

        private void ClientsView_SelectClient(object sender, ClientEventArgs e)
        {
            ServersView.LaunchWithClient(e.Client);
            PropertiesView.Entity = e.Client;
        }

        private void ClientsView_EditClient(object sender, ClientEventArgs e)
        {
            MainSplitContainer.Panel2Collapsed = false;
            PropertiesView.Entity = e.Client;
        }

        private void ClientsView_AddClient(object sender, ClientEventArgs e)
        {
            _CurrentConfig.ClientConfig.Clients.Add(e.Client);

            MainSplitContainer.Panel2Collapsed = false;
            PropertiesView.Entity = e.Client;
        }

        private void ClientsView_DeleteClient(object sender, ClientEventArgs e)
        {
            _CurrentConfig.ClientConfig.Clients.Remove(e.Client);
        }

        private void PropertiesView_Collapse(object sender, EventArgs e)
        {
            MainSplitContainer.Panel2Collapsed = true;
        }

        private void ClientsView_DetectedClients(object sender, ClientsEventArgs e)
        {
            EntityList<Client> Clients = new EntityList<Client>((IEnumerable<Client>)_CurrentConfig.ClientConfig.Clients);

            foreach (Client OneClient in e.Clients)
                if (!Clients.Exists(delegate(Client client) { return client.Name == OneClient.Name; }))
                    Clients.AddRange(e.Clients);

            ClientsView.Clients = _CurrentConfig.ClientConfig.Clients;
        }
    }
}
